package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<ShopType> listCache() {
        List<ShopType> list;
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String shop_typeJson = opsForValue.get("shop-type");

        if (StrUtil.isNotBlank(shop_typeJson)) {
            //1找redis，成功
            list = JSONUtil.toList(shop_typeJson, ShopType.class);
            return list;
        }
        
        //2 失败 找数据库
        list = this.list();
        String value = list.toString();
        opsForValue.set("shop-type",value);

        return list;
    }
}
